package com.test.demo.test;

import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

/**
 * 生产者
 * @author Gao
 * @date 2020/8/27
 */
public class RocketProducer {

    public static void main(String[] args) {
        //1.创建一个生产者,需要指定Producer的分组
        // public DefaultMQProducer(String producerGroup)
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer("producerGroupTest");
        //2.设置命名服务的地址,默认是去读取conf文件下的配置文件 rocketmq.namesrv.addr
        defaultMQProducer.setNamesrvAddr("81.70.58.152:9876");
        try {
            //3.启动生产者
            defaultMQProducer.start();
            for (int i = 0; i < 2; i++) {
                String text = " Hello RocketMq----" + i;
                //4.最基本的生产模式 topic + 文本信息
                // public Message(String topic, String tags, byte[] body)
                Message msg = new Message("topic_orderCreate", "Tag-B111", text.getBytes());
                //5.获取发送响应
                // public SendResult send(Message msg) throws MQClientException, RemotingException, MQBrokerException, InterruptedException
                SendResult sendResult = defaultMQProducer.send(msg);
                System.out.println("发送结果 result:" + JSON.toJSONString(sendResult));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //6.释放生产者
            defaultMQProducer.shutdown();
        }
    }
}
